﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ATW.MT.BUS;
using System.Data;
using System.Text;

namespace ATW.MT.SellManage
{
    public partial class _editSupply : System.Web.UI.Page
    {
        bus_T_Supply TS = new bus_T_Supply();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserInfo"] == null)
            {
                Response.Redirect("../Main/Login.aspx");
            }
            if (!IsPostBack)
            {
                BindData();
            }
        }
        private void BindData()
        {//加载
            if (!string.IsNullOrEmpty(Request.QueryString["uid"]))
            {
                string tauloginID = Request.QueryString["uid"];
                if (!CommonClass.common.filterChar(tauloginID))
                {
                    return;
                }
                DataTable dt = TS.SupplySingleQuery(tauloginID);
                if (dt != null && dt.Rows.Count > 0)
                {
                    txtTauName.Text = dt.Rows[0]["tauName"].ToString();
                    txtCotName.Text = dt.Rows[0]["companyName"].ToString();
                    txtLevel.Text = dt.Rows[0]["level"].ToString();
                }
            }
        }
        protected void lbnSave_Click(object sender, EventArgs e)
        {//保存
            try
            {
                string tauloginID = Request.QueryString["uid"];
                string name = txtTauName.Text.Trim();
                string companyName = txtCotName.Text.Trim();
                string level = txtLevel.Text.Trim();
                int _level = 0;
                if (name == "")
                {
                    Tips("供应商名称不能为空！");
                    return;
                }
                if (level == "")
                {
                    Tips("供应商等级不能为空！");
                    return;
                }
                if (!CommonClass.common.filterChar(name))
                {
                    Tips("供应商名称中含有危险字符！");
                    return;
                }
                if (!CommonClass.common.filterChar(companyName))
                {
                    Tips("公司名称中含有危险字符!");
                    return;
                }
                if (!CommonClass.common.filterChar(tauloginID))
                {
                    return;
                }
                if (!int.TryParse(level, out _level) || _level <= 0)
                {
                    Tips("等级需要为整数");
                    return;
                }
                int result = TS.SupplyEdit(name, companyName, level, tauloginID);
                if (result > 0)
                {
                    CloseWindowRefresh();
                }
                else
                {
                    Tips("新增失败！");
                }
            }
            catch (Exception ex)
            {

            }
        }
        private void Tips(string s)
        {
            StringBuilder strScript = new StringBuilder();
            strScript.Append(@"<script language=javascript>
                var divTips=new jSharp.popUpDiv('divMsg');
                document.getElementById('msgContent').innerHTML='" + s + @"';
                divTips.Show();
                var t=setTimeout(function(){
                    divTips.Close();
                    clearTimeout(t);
                },1000);
            </script>");
            this.ClientScript.RegisterStartupScript(this.GetType(), "tips", strScript.ToString());
        }//提示
        private void CloseWindowRefresh()
        {
            string strScript = "<script language=javascript>";
            strScript += "if(window.opener){";
            strScript += "window.opener.location.href=window.opener.location.href";
            strScript += "};";
            strScript += "window.close()";
            strScript += "</script>";
            this.ClientScript.RegisterStartupScript(this.GetType(), "closeWin", strScript.ToString());
        }
    }
}